package com.citrix.gs.util.beacon;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Perforce extends Scm {
    
	private String host;
    private String port;
    private Map<String, String> viewMap;
    private List<String> viewMapList;

    public String getViewMapString() {
        
        if(viewMapList != null) {
            return viewMapList.stream().reduce((v1, v2) -> {
                return v1 + "\n" + v2;
            }).get();
        }
        
        if(viewMap != null) {
            return viewMap.keySet().stream().map(k -> {
                return k + " " + viewMap.get(k);
            }).reduce((v1, v2) -> {
                return v1 + "\n" + v2;
            }).get();
        }
            
        return "";  
    }
    
    public String getViewMapString(String workSpace) {
    
        String viewMapString = this.getViewMapString();
        return viewMapString.replaceAll("\\{workspace\\}", workSpace);
    }
    

	public Perforce() {
        super(ScmType.SCM_TYPE_PERFORCE);  
        setHost("nkgp401.eng.citrite.net"); // TODO: fix the hardcode
        setPort("2444");
        viewMap = null;
        viewMapList = null;
    }
    
    public Perforce(String port) {
        this();
        this.port = port;
    }
    
    public Perforce(String host,String port) {
        this(port);
        this.setHost(host);
    }
    
    public String getServer(){
    	return String.format("%s:%s", host, port);
    }

    public String getPort() {
        return port;
    }

    public Perforce setPort(String port) {
        this.port = port;
        return this;
    }

    public Map<String, String> getViewMap() {
        return viewMap;
    }

    public Perforce setViewMap(Map<String, String> viewMap) {
        this.viewMap = viewMap;
        return this;
    }
    
    public Perforce setViewMap(List<String> viewMapList) {
        this.viewMap = viewMapList.stream().collect(Collectors.toMap(s -> {
            return s.split(" +")[0];
        }, s -> {
            return s.split(" +")[1];
        }));
        return this;
    }

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}
}
